﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using HYElectricDim;

using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.WirePlace
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdSetValueToWire : IExternalCommand
	{
		public static void AddSharedParamsToWire(Document doc, Wire wire)
		{
			Dictionary<string, ParameterType> dictionary = new Dictionary<string, ParameterType>();
            dictionary.Add("截面", (ParameterType)3);
            dictionary.Add("敷设方式", (ParameterType)1);
            dictionary.Add("相对标高", (ParameterType)3);
            dictionary.Add("线管类型", (ParameterType)1);
            dictionary.Add("管径", (ParameterType)1);
			Dictionary<string, ParameterType> dictionary2 = new Dictionary<string, ParameterType>();
            dictionary2.Add("线管类型ID", (ParameterType)1);
			foreach (KeyValuePair<string, ParameterType> keyValuePair in dictionary)
			{
				if (wire.GetParameter(keyValuePair.Key) == null)
				{
					YArchitech.Revit.Electric.Common.SharedParameter.InsertSharedParameter(doc, BuiltInCategory.OST_Wire, "电气数据", dictionary, true);
					break;
				}
			}
			foreach (KeyValuePair<string, ParameterType> keyValuePair2 in dictionary2)
			{
				if (wire.GetParameter(keyValuePair2.Key) == null)
				{
					YArchitech.Revit.Electric.Common.SharedParameter.InsertSharedParameter(doc, BuiltInCategory.OST_Wire, "电气数据", dictionary2, false);
					break;
				}
			}
			wire.GetParameter(dictionary.ElementAt(0).Key).Set(WireConnectForm.wireParams.CrossSection);
			wire.GetParameter(dictionary.ElementAt(1).Key).Set(WireConnectForm.wireParams.LayMode.ToString());
			wire.GetParameter(dictionary.ElementAt(2).Key).Set(WireConnectForm.wireParams.Level);
			wire.GetParameter(dictionary.ElementAt(3).Key).Set(WireConnectForm.wireParams.ConduitType.ToString());
			wire.GetParameter(dictionary.ElementAt(4).Key).Set(WireConnectForm.wireParams.ConduitDiameter / 304.8);
			wire.GetParameter(dictionary2.ElementAt(0).Key).Set(WireConnectForm.wireParams.ConduitTypeID.ToString());
		}

		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.ActiveView.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((HYView.CmdViewType)3, cmdData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			Wire wire = null;
			try
			{
                Reference reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ElectricDim.WireFilter(), "选择基准导线。");
				wire = (document.GetElement(reference) as Wire);
			}
			catch
			{
			}
			try
			{
				using (DlgWireParam dlgWireParam = new DlgWireParam(document, wire))
				{
					dlgWireParam.Show(WindowHelper.HWndRevit);
					WindowHelper.SendMessageToWndRevit();
					if (dlgWireParam.Failed)
					{
						MessageBox.Show(dlgWireParam.FailedRes);
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
					filteredElementCollector.OfClass(typeof(WireType));
					IList<Element> list = filteredElementCollector.ToElements();
					for (;;)
					{
						Transaction transaction = new Transaction(document, "setWire");
						try
						{
							transaction.Start();
							foreach (Reference reference2 in activeUIDocument.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new WireFilter(), "选择需要赋值的导线。"))
							{
								Wire wire2 = document.GetElement(reference2) as Wire;
								NewConnectWire.AddSharedParamsForWire(document, wire2);
								wire2.HotConductorNum = Convert.ToInt32(dlgWireParam.nudWireNum.Value);
								wire2.GroundConductorNum = 1;
								wire2.NeutralConductorNum = 1;
								foreach (Element element in list)
								{
									if (element.Name == dlgWireParam.cmbWireType.Text)
									{
										WireType wireType = (WireType)element;
										wire2.ChangeTypeId(wireType.Id);
									}
								}
								wire2.GetParameter("相对标高").Set(dlgWireParam.txtLevel.Text);
								wire2.GetParameter("敷设方式").Set(dlgWireParam.cmbLayMode.Text);
								wire2.GetParameter("截面").Set(dlgWireParam.cmbCrossSection.Text);
								wire2.GetParameter("线管类型").Set(dlgWireParam.cmbConduitType.Text);
							}
							transaction.Commit();
							continue;
						}
						catch (Exception)
						{
							transaction.RollBack();
							dlgWireParam.Close();
						}
						break;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
